package Datalog

class DatalogPath(givenAtoms: Set[DatalogAtom], s: DatalogTerm, dest: DatalogTerm) {
  require { isValid(givenAtoms) }

  val start = s
  val destination = dest
  val atoms = givenAtoms

  def this(atom: DatalogAtom) = {
    this(Set(atom), atom.terms(0), atom.terms(1))
  }

  def combine(other: DatalogPath): DatalogPath = {
    if (!this.atoms.intersect(other.atoms).isEmpty) this
    else new DatalogPath(this.atoms.union(other.atoms), this.start, other.destination)
  }

  def reverse(): DatalogPath = {
    new DatalogPath(this.atoms, this.destination, this.start)
  }

  def isValid(atoms: Set[DatalogAtom]): Boolean = {
    if (atoms.isEmpty) true
    else if (atoms.head.pred.arity == 2) isValid(atoms.tail)
    else false
  }

  def isSimilarTo(path: DatalogPath): Boolean =
    this.start == path.start && this.destination == path.destination

  override def toString() = "[" + start + ", " + destination /* + ", " + atoms */ + "]"
}